package mbp.gui.panels;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTable;

import mbp.agents.MbpManagerAgent;
import mbp.base.GuiFunction;
import mbp.base.MessageType;
import mbp.core.Resource;
import mbp.core.ResourceUtility;
import mbp.gui.frames.ResourceEditor;
import mbp.gui.frames.ResourceUtilityEditor;
import mbp.message.MessageXml;
import javax.swing.ListSelectionModel;

public class ObjectViewerGui extends JPanel implements ActionListener
{
	private MbpManagerAgent myAgent;
	private JTextField txtAgentName;
	private JButton btnGetObjectsList;
	private DefaultTableModel RcDetailsModel;
	private ResourceListPanel rcViewer;
	private ValidationListPanel rcvViewer;
	private UtilityListPanel rcuViewer;
	
	public ObjectViewerGui(MbpManagerAgent mbpManager) 
	{
		myAgent = mbpManager;
		setLayout(null);
		
		
		rcViewer = new ResourceListPanel() {
			public void processDoubleClick(int index)
			{
				getDetailsRC(index);
			}
		};
		rcViewer.setBorder(new TitledBorder(null, "Resources list", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		rcViewer.setBounds(10, 91, 422, 270);
		add(rcViewer);
		
		rcvViewer = new ValidationListPanel() {
			public void processDoubleClick(int index)
			{
				getDetailsRCV(index);
			}
		};
		rcvViewer.setBorder(new TitledBorder(null, "Validation functions list", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		rcvViewer.setBounds(442, 11, 300, 165);
		add(rcvViewer);
		
		rcuViewer = new UtilityListPanel() {
			public void processDoubleClick(int index)
			{
				getDetailsRCU(index);
			}
		};
		rcuViewer.setBorder(new TitledBorder(null, "Utility functions list", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		rcuViewer.setBounds(442, 187, 300, 174);
		add(rcuViewer);
		
		
		JPanel panel0 = new JPanel();
		panel0.setBorder(new TitledBorder(null, "Agent", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel0.setBounds(10, 11, 422, 69);
		add(panel0);
		panel0.setLayout(null);
		
		txtAgentName = new JTextField();
		txtAgentName.setBounds(10, 38, 120, 20);
		panel0.add(txtAgentName);
		txtAgentName.setColumns(10);
		
		JLabel lblAgentName = new JLabel("Agent name");
		lblAgentName.setBounds(10, 21, 100, 14);
		panel0.add(lblAgentName);
		
		btnGetObjectsList = new JButton("Get");
		btnGetObjectsList.setBounds(140, 35, 91, 23);
		panel0.add(btnGetObjectsList);
		btnGetObjectsList.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == btnGetObjectsList)
		{
			myAgent.SendObjectLGet(this.txtAgentName.getText(), MessageType.RESOURCELGET);
			myAgent.SendObjectLGet(this.txtAgentName.getText(), MessageType.UTILITYLGET);
			myAgent.SendObjectLGet(this.txtAgentName.getText(), MessageType.VALIDATIONLGET);
		}
		// TODO Auto-generated method stub
		
	}
	
	public void getDetailsRC(int selected)
	{
		if(selected==-1)return;
		
		String uuid = (String) rcViewer.getTable().getValueAt(selected, 2);
		String type = (String) rcViewer.getTable().getValueAt(selected, 0);
		
		myAgent.SendResourceGet(this.txtAgentName.getText(), type, uuid);
	}
	
	public void getDetailsRCV(int selected)
	{
		if(selected==-1)return;
		
		String type = (String) rcvViewer.getTable().getValueAt(selected, 0);
		//myAgent.send
	}
	
	public void getDetailsRCU(int selected)
	{
		//System.out.println("index=" + selected);
		if(selected==-1)return;
		
		String type = (String) rcuViewer.getTable().getValueAt(selected, 0);
		myAgent.SendUtilityGet(this.txtAgentName.getText(), type);
	}
	
	public synchronized void reloadResourcesList(MessageXml messageXml)
	{
		if(myAgent == null)return;
		
		rcViewer.reloadListInfo(messageXml.resources);
		
	}
	
	public synchronized void reloadUtilityList(MessageXml messageXml)
	{
		if(myAgent == null)return;
		
		//rcViewer.reloadResourcesList1(messageXml.resources);
		rcuViewer.reloadListInfo(messageXml.utilities);
		
	}
	
	public synchronized void reloadValidationList(MessageXml messageXml)
	{
		if(myAgent == null)return;
		
		//rcViewer.reloadResourcesList1(messageXml.resources);
		rcvViewer.reloadListInfo(messageXml.validations);
		
	}
	
	public void loadResource(Resource rc)
	{
		ResourceEditor resourceEditor = new ResourceEditor(rc, GuiFunction.VIEWER, null);
		resourceEditor.setVisible(true);
	}
	
	public void loadUtility(ResourceUtility ru)
	{	
		ResourceUtilityEditor rcuEdit = new ResourceUtilityEditor(ru, GuiFunction.VIEWER, null);
		rcuEdit.setVisible(true);
	}
}
